//
//  XJMyWalletViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/23.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

// 这个类是直接写在 XJMyWalletViewController.h 他的头文件就是 XJMyWalletViewController.h
@interface XJMyWalletTableViewCellModel:NSObject
/**1.icon*/
@property (nonatomic, copy) NSString *icon;
/**2.标题*/
@property (nonatomic, copy) NSString *name;

@end
@implementation XJMyWalletTableViewCellModel
@end

#import "XJMyWalletViewController.h"
//Controller
#import "XJWalletChargeViewController.h"
#import "XJWalletFinanceDetailViewController.h"
#import "XJWalletTiXianAccountViewController.h"
#import "XJPayPasswordViewController.h"
#import "XJTixianViewController.h"//提现

//view
#import "XJMyWalletCell.h"
#import "XJMyWalletTableHeaderView.h"


@interface XJMyWalletViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong) UITableView *tableView;

@property(nonatomic, strong) NSMutableArray *dataArray;
@property(nonatomic, strong) XJMyWalletTableHeaderView *headerView;

@end

@implementation XJMyWalletViewController
#pragma mark - 一 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navTitle = @"我的钱包";
    [self requestCustomDetailData];
    
    [self setupView];
    
    [self moniRequestData];
}

- (void)moniRequestData {
    
    NSArray *tempArr = @[@{@"icon":@"order_fahuo",@"name":@"查看资金明细"},
                         @{@"icon":@"order_fukuan",@"name":@"提现账户管理"},
                         @{@"icon":@"order_pingjia",@"name":@"支付密码"},
                         @{@"icon":@"order_tuikuan",@"name":@"提现"},];
    
    for (NSDictionary *dic in tempArr) {
        XJMyWalletTableViewCellModel *model = [XJMyWalletTableViewCellModel mj_objectWithKeyValues:dic];
        
        [self.dataArray addObject:model];
    }
    
    // 刷新
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}

- (void)setupView {
    [self.view addSubview:self.tableView];
    
    // 设置tableView 的表头表尾
    self.tableView.tableFooterView = [UIView new];
    
    self.headerView.frame = CGRectMake(0, 0, KWidth, 251);
    self.tableView.tableHeaderView = self.headerView;
}

#pragma mark - 二 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KWidth, KHeight-XJTopNavH) style:(UITableViewStylePlain)];
        [_tableView registerClass:[XJMyWalletCell class] forCellReuseIdentifier:@"XJMyWalletCellId"];
        
        _tableView.dataSource =self;
        _tableView.delegate = self;
    }
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (XJMyWalletTableHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[XJMyWalletTableHeaderView alloc] init];
        
        XJWeakSelf(self);
        _headerView.myWalletTableViewHeaderVeiwChargeBlock = ^{
            XJWalletChargeViewController *chargeVC = [[XJWalletChargeViewController alloc] init];
            [weakself.navigationController pushViewController:chargeVC animated:YES];
        };
    }
    return _headerView;
}

#pragma mark - 三 UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XJMyWalletCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XJMyWalletCellId"];
    
    XJMyWalletTableViewCellModel *model = self.dataArray[indexPath.row];
    [cell configMyWalletCellWithIcon:model.icon name:model.name];
    
    return cell;
}

#pragma mark -  UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) { //账户明细
        
        XJWalletFinanceDetailViewController *financeVC = [[XJWalletFinanceDetailViewController alloc] init];
        [self.navigationController pushViewController:financeVC animated:YES];
    }else if (indexPath.row == 1) { // 提现账户管理
        
        XJWalletTiXianAccountViewController *tixianAccountVC = [[XJWalletTiXianAccountViewController alloc] init];
        [self.navigationController pushViewController:tixianAccountVC animated:YES];
    }else if (indexPath.row == 2){ // 支付密码
        XJPayPasswordViewController * payPwdVC = [[XJPayPasswordViewController alloc] init];
        [self.navigationController pushViewController:payPwdVC animated:YES];
    }else { // 提现
        
        XJTixianViewController *tiXianVC = [[XJTixianViewController alloc] init];
        
        [self.navigationController pushViewController:tiXianVC animated:YES];
    }
}


#pragma mark - 四 请求数据
- (void)requestCustomDetailData {
    [XJHttpTool requestCustomDetailSuccesH:^(id responseObjc) {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObjc options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        
        XJLog(@"%@",jsonStr);
        
        
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
           NSNumber *balacen = responseObjc[@"data"][@"customer"][@"balance"];
            
            [self.headerView congifureMyWalletHeaderViewWithBalance:balacen];
        }
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}


@end
